﻿package extremefx.geom {
	import flash.geom.Point;	
	
	/**
	 * Point Inclusion in Polygon Test
	 * @author Marcelo Volmaro
	 */
	public final class PnPoly {
		/**
		 * @param pPolygon an ordered array of <code>Point</code>s that delimits the polygon
		 * @param pPoint the <code>Point</code> to check if it is inside the polygon or not.
		 * @return true if the point is inside the polygon, false if it is not.
		 */
		static public function check(pPolygon:Vector.<Point>, pPoint:Point):Boolean {
			var i:int, j:int, c:Boolean = false, npol:int = pPolygon.length, point1:Point, point2:Point;
			var x:int = pPoint.x, y:int = pPoint.y;
			
			for (i = 0, j = npol - 1; i<npol; j = i++){
				point1 = pPolygon[i];
				point2 = pPolygon[j];
				
				if ((((point1.y <= y) && (y < point2.y)) || ((point2.y <= y) && (y < point1.y))) &&
            		(x < (point2.x - point1.x) * (y - point1.y) / (point2.y - point1.y) + point1.x)) c = !c;
				
			}
			
			return c;
		}
	}
}

      